package sskj.regulatory.presentation.base_mvp.base

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View

import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import sskj.regulatory.R
import sskj.regulatory.arch.BaseMVPFragment
import sskj.regulatory.di.component.ActivityComponent
import sskj.regulatory.presentation.screen.launcher.LauncherActivity
import sskj.regulatory.presentation.screen.main.MainActivity


abstract class BaseFragment<V : BaseContract.View, P : BaseContract.Presenter<V>> : BaseMVPFragment<V, P>(), BaseContract.View {

    lateinit var activityComponent: ActivityComponent

    lateinit var activity: BaseActivity<*, *>

    @SuppressLint("MissingSuperCall")
    override fun onAttach(context: Context?) {
        super.onAttach(context)
        if (context is BaseActivity<*, *>) {
            activity = context
            activityComponent = activity.activityComponent
            injectDependencies()
        }
    }

    override fun connectError() {

        QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("错误提示")
                .setMessage("服务器异常,无法连接,请稍后再试或者联系客服。")
                .addAction("确定") { dialog, _ ->
                    activity.tipDialog?.dismiss()
                    dialog.dismiss()
                }
                .create(R.style.QMUI_Dialog).show()
    }

    override fun sessionError() {
        QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("提示信息")
                .setMessage("登录超时,点击确定返回登录页面重新验证身份信息。")
                .addAction("确定") { dialog, _ ->
                    activity.tipDialog?.dismiss()
                    dialog.dismiss()
                    startActivity(Intent(context, LauncherActivity::class.java))
//                    getActivity()?.finish()
                }
                .create(R.style.QMUI_Dialog).show()
    }


    override fun backViewInitOffset(): Int {
        return QMUIDisplayHelper.dp2px(context, 100)
    }


    override fun onCreateView(): View {
        return LayoutInflater.from(getActivity()).inflate(layoutResId, null);
    }


    protected abstract fun injectDependencies()

    protected abstract val layoutResId: Int


}